package com.kavian.test.image;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;

import javax.imageio.ImageIO;

/**  
 * @Filename:    MyTools→com.kavian.test.image→ScaleImage.java
 * @Description:   缩放图片
 * @Copyright:   Copyright (c)2012 
 * @Company:     qoocc 
 * @author:   	kavian 
 * @version:    1.0  
 * Create at:  2012-6-29下午1:58:43
 *  
 * Modification History:  
 * Date                      Author           Version     Description  
 * ------------------------------------------------------------------  
 * 2012-6-29下午1:58:43   	     Administrator          1.0        1.0 Version  
 */
public class ScaleImage{
	
	/**
	 * @description 	图片缩放
	 * @param file 缩放图片
	 * @param scale 放大系数
	 * kavian  2012-6-28上午9:10:14	
	 * @throws Exception 
	 */
	public static void scale(File file,double scale) throws Exception{
		
		String endName = file.getName();
		endName = endName.substring(endName.lastIndexOf(".") + 1);
		
		BufferedImage src = ImageIO.read(file); // 读入文件
		int width = src.getWidth(); // 得到源图宽  
        int height = src.getHeight(); // 得到源图长
        
        width = (int)(width * scale);  
        height = (int)(height * scale);
        
        Image image = src.getScaledInstance(width, height, Image.SCALE_DEFAULT);  
        BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);  
        Graphics g = tag.getGraphics();  
        g.drawImage(image, 0, 0, null); // 绘制缩小后的图  
        g.dispose();  
        ImageIO.write(tag, endName, file);// 输出到文件流
	}
	
	public static void main(String[] args) throws Exception {
		File file = new File("d://1.jpg");
		scale(file,0.8);
	}
}